<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/message.css">
</head>

<body>
    <div>
        <h1>留言板</h1>
        <ul></ul>
        <textarea placeholder="请编辑您的留言"></textarea>
        <button class="sub">发布</button>
    </div>
    <script>
        var button = document.querySelector('button');
        var text = document.querySelector('textarea');
        var ul = document.querySelector('ul');
        button.onclick = function() {
            if (text.value == '') {
                alert('您没有输入内容');
            } else {
                var li = document.createElement('li');
                li.innerHTML = text.value + "<a href='javascript:;'>删除</a>";
                ul.insertBefore(li, ul.children[0]);
                var a = document.querySelectorAll('a');
                for (var i = 0; i < a.length; i++) {
                    a[i].onclick = function() {
                        ul.removeChild(this.parentNode);
                    }
                }
            }
        }
    </script>
</body>


</html>